package com.ahdms.es.util;

import com.ahdms.es.bean.CertInfo;
import com.ahdms.es.bean.SealInfo;
import com.ahdms.es.gm.v1.SESeal;
import com.ahdms.es.gm.v4.SES_Signature;
import org.bouncycastle.asn1.x509.Certificate;

import java.text.ParseException;

/**
 * @author qinxiang
 * @date 2021-04-16 16:15
 */
public class DataConvert {

    public static CertInfo certToCertInfo(Certificate cert) {
        String startDate = DateUtils.format(cert.getStartDate().getDate());
        String endDate = DateUtils.format(cert.getEndDate().getDate());
        String version = cert.getVersion().getValue().toString();
        String subject = cert.getSubject().toString();
        String issuer = cert.getIssuer().toString();
        String serialNum = cert.getSerialNumber().getValue().toString();
        String algorithm = cert.getSignatureAlgorithm().getAlgorithm().getId();
        return CertInfo.bulider().version(version).algorithm(algorithm)
                .serialNum(serialNum).subjectDN(subject).issuerDN(issuer).notBefore(startDate).notAfter(endDate).build();

    }

    public static SealInfo toSealInfo(SESeal seSeal) {
        String sealCode = seSeal.getEsealInfo().getEsID().getString();
        String sealName = seSeal.getEsealInfo().getProperty().getName().getString();
        String createDate = DateUtils.format(seSeal.getEsealInfo().getProperty().getCreateDate());
        String validStartDate = DateUtils.format(seSeal.getEsealInfo().getProperty().getValidStart());
        String validEndDate = DateUtils.format(seSeal.getEsealInfo().getProperty().getValidEnd());
        String vid = seSeal.getEsealInfo().getEsID().getString();
        return SealInfo.builder().sealCode(sealCode).sealName(sealName)
                .createDate(createDate).vid(vid)
                .validStartTime(validStartDate).validEndTime(validEndDate)
                .build();
    }


    public static SealInfo toSealInfo(com.ahdms.es.gm.v4.SESeal seSeal) {
        String sealCode = seSeal.geteSealInfo().getEsID().getString();
        String sealName = seSeal.geteSealInfo().getProperty().getName().getString();
        String createDate = DateUtils.format(seSeal.geteSealInfo().getProperty().getCreateDate());
        String validStartDate = DateUtils.format(seSeal.geteSealInfo().getProperty().getValidStart());
        String validEndDate = DateUtils.format(seSeal.geteSealInfo().getProperty().getValidEnd());
        String vid = seSeal.geteSealInfo().getEsID().getString();
        return SealInfo.builder().sealCode(sealCode).sealName(sealName)
                .createDate(createDate).vid(vid)
                .validStartTime(validStartDate).validEndTime(validEndDate)
                .build();
    }

}
